package cn.huaqingcheng.tianshu.core.uc.service;

import cn.huaqingcheng.tianshu.core.uc.dto.AccountParam;
import cn.huaqingcheng.tianshu.core.uc.model.Account;
import cn.huaqingcheng.tianshu.core.uc.repository.AccountRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * AccountServiceImpl
 */
@Service
@RequiredArgsConstructor
public class AccountServiceImpl implements AccountService {

    private final AccountRepository accountRepository;


    @Override
    @Transactional(readOnly = true)
    public Optional<Account> getByUsername(String username) {
        return accountRepository.getByUsername(username);
    }

    @Override
    @Transactional(readOnly = true)
    public Optional<Account> getById(Long id) {
        return accountRepository.getById(id);
    }

    @Override
    public Account updateAccount(Long accountId, AccountParam accountParam) {
        Account account = new Account()
                .setId(accountId)
                .setNickname(accountParam.getNickname())
                .setAvatar(accountParam.getAvatar())
                .setEmail(accountParam.getEmail())
                .setMobile(accountParam.getMobile())
                .setVersion(accountParam.getVersion());
        return accountRepository.updateById(account);
    }

}
